package com.flyaudio.xiaopengautopilot.activity;

import android.app.Activity;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.View;
import android.view.WindowManager;
import android.view.animation.AnimationSet;
import android.view.animation.TranslateAnimation;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ScrollView;
import android.widget.Toast;

import androidx.constraintlayout.widget.ConstraintLayout;

import com.flyaudio.xiaopengautopilot.R;
import com.flyaudio.xiaopengautopilot.center.VehicleService;
import com.flyaudio.xiaopengautopilot.constant.JSONKeyName;
import com.flyaudio.xiaopengautopilot.custom.ButtonWindows;
import com.flyaudio.xiaopengautopilot.custom.FlySeatView;
import com.flyaudio.xiaopengautopilot.presenter.IActivityCallback;
import com.flyaudio.xiaopengautopilot.presenter.IVehicleModule;
import com.flyaudio.xiaopengautopilot.util.DataInfo;
import com.flyaudio.xiaopengautopilot.util.JSONUtil;
import com.flyaudio.xiaopengautopilot.util.L;
import com.flyaudio.xiaopengautopilot.constant.ModuleKey;
import com.flyaudio.xiaopengautopilot.constant.MsgViewUtil;
import com.flyaudio.xiaopengautopilot.util.ToastUtils;

import java.util.Timer;
import java.util.TimerTask;

public class ControlActivity extends Activity implements View.OnClickListener, View.OnTouchListener, IActivityCallback {
    private final String TAG = "ControlActivity";

    private ScrollView control_cl;
    private Button control_light_off, control_light_auto, control_light_width,
            control_light_low_beam, control_light_rear_rog;
    private Button control_light_home;

    private ButtonWindows control_lock_off, control_lock_breathable, control_lock_ventilation;
    private ButtonWindows control_lock_rearview_mirror, control_lock_trunk;
    private Button control_lock_unlock;

    private Button control_pilot_driving_standard, control_pilot_driving_motion, control_pilot_driving_economic;
    private Button control_pilot_energy_low, control_pilot_energy_mid, control_pilot_energy_high;

    private ButtonWindows control_seat_oblique_left,control_seat_oblique_down;
    private ButtonWindows control_seat_up,control_seat_down;
    private ButtonWindows control_seat_left,control_seat_right;

    private ConstraintLayout control_oblique_cl,control_left_right_cl,control_up_down_cl;
    private FlySeatView control_seat_iv;

    private float moveX=0,moveY=0, moveZ=0;

    private int clickDrivingMode=1;//点击驾驶模式
    private int clickErvLevel=0;//点击能量回收等级
    private int autoLight = 0;
    private int highBeamLight=0;
    private int lowBeamLight, widthLight = 0;
    private int goHomeLight, rFogLight, rWidthLight = 0;
    private int lfWindow_up, lfWindow_down = 0;
    private int rfWindow_up, rfWindow_down = 0;
    private int lrWindow_up, lrWindow_down = 0;
    private int rrWindow_up, rrWindow_down = 0;
    private int autoWindow = 0;
    private int breathable = 0;
    private int trunk = 0;
    private int outerMirror_lf, outerMirror_rf = 0;
    private int lfDoorLock, rfDoorLock, lrDoorLock, rrDoorLock = 0;
    private int driverSeatAdjust = 0;
    private int driverSeatWindLevel = 0, driverSeat_WarnLevel = 0;
    private int frontWiperGear = 0;
    private int rearWiperSw = 0;
    private int walkWarnSw = 0, walkWarnEn = 0;
    private int autoSafetyBeltSw = 0, rearSafetyBeltWarnSw = 0;
    private int frontHorn = 0; //喇叭
    private int byteValid = 0; //有效位

    private ImageView control_auto_light_bg, control_auto_light_thumb,control_auto_light_content;
    private AnimationSet animationSet;
    private int autolightThumbX = 0;

    private double carSpeed=0.0;
    private ToastUtils toastUtils;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            getWindow().clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
            getWindow().setStatusBarColor(getResources().getColor(R.color.colorStatusBarColor));
        }
        setContentView(R.layout.activity_control);
        initView();
        control_lock_trunk.setClick_enable(true);
        clickDrivingMode = (int) JSONUtil.getInstance().getMileageJSON(JSONKeyName.DrivingMode,1);
        clickErvLevel = (int) JSONUtil.getInstance().getMileageJSON(JSONKeyName.EnergyRecovery,0);
        if(VehicleService.service != null) {
            VehicleService.service.MakeAndSendMessage(1, senPilotMsgToService());
        }
        updateHandler.postDelayed(r,1000);

        toastUtils = new ToastUtils();
        L.d(TAG, "onCreate");
    }

    @Override
    protected void onResume() {
        super.onResume();
        IVehicleModule.getInstance().setCallBack(ControlActivity.this);
        updateView();
        L.d(TAG, "onResume");
    }

    @Override
    protected void onPause() {
        super.onPause();
        IVehicleModule.getInstance().setCallBack(null);

        L.d(TAG, "onPause");
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        updateHandler.removeCallbacks(r);
        L.d(TAG, "onDestroy");
    }

    private void initView() {
        control_cl = findViewById(R.id.control_cl);
        control_auto_light_bg = findViewById(R.id.control_auto_light_bg);
        control_auto_light_thumb = findViewById(R.id.control_auto_light_thumb);
        control_auto_light_content = findViewById(R.id.control_auto_light_content);
        control_auto_light_bg.setOnTouchListener(this);

        control_light_off = findViewById(R.id.control_light_off);
//        control_light_auto = findViewById(R.id.control_light_auto);
//        control_light_width = findViewById(R.id.control_light_width);
//        control_light_low_beam = findViewById(R.id.control_light_low_beam);
        control_light_rear_rog = findViewById(R.id.control_light_rear_rog);
        control_light_home = findViewById(R.id.control_light_home);
        control_light_off.setOnClickListener(this);
       // control_light_auto.setOnClickListener(this);
      //  control_light_width.setOnClickListener(this);
     //   control_light_low_beam.setOnClickListener(this);
        control_light_rear_rog.setOnClickListener(this);
        control_light_home.setOnClickListener(this);

        control_lock_off = findViewById(R.id.control_lock_off);
        control_lock_breathable = findViewById(R.id.control_lock_breathable);
        control_lock_ventilation = findViewById(R.id.control_lock_ventilation);
        control_lock_rearview_mirror = findViewById(R.id.control_lock_rearview_mirror);
        control_lock_unlock = findViewById(R.id.control_lock_unlock);
        control_lock_trunk = findViewById(R.id.control_lock_trunk);
        control_lock_off.setOnTouchListener(this);
        control_lock_breathable.setOnTouchListener(this);
        control_lock_ventilation.setOnTouchListener(this);
        control_lock_rearview_mirror.setOnTouchListener(this);
        control_lock_unlock.setOnTouchListener(this);
        control_lock_trunk.setOnTouchListener(this);
        control_pilot_driving_standard = findViewById(R.id.control_pilot_driving_standard);
        control_pilot_driving_motion = findViewById(R.id.control_pilot_driving_motion);
        control_pilot_driving_economic = findViewById(R.id.control_pilot_driving_economic);
        control_pilot_energy_low = findViewById(R.id.control_pilot_energy_low);
        control_pilot_energy_mid = findViewById(R.id.control_pilot_energy_mid);
        control_pilot_energy_high = findViewById(R.id.control_pilot_energy_high);
        control_pilot_driving_standard.setOnClickListener(this);
        control_pilot_driving_motion.setOnClickListener(this);
        control_pilot_driving_economic.setOnClickListener(this);
        control_pilot_energy_low.setOnClickListener(this);
        control_pilot_energy_mid.setOnClickListener(this);
        control_pilot_energy_high.setOnClickListener(this);

        control_oblique_cl = findViewById(R.id.control_oblique_cl);
        control_left_right_cl= findViewById(R.id.control_left_right_cl);
        control_up_down_cl= findViewById(R.id.control_up_down_cl);
        control_seat_iv = findViewById(R.id.control_seat_iv);
        control_seat_oblique_left = findViewById(R.id.control_seat_oblique_left);
        control_seat_oblique_down = findViewById(R.id.control_seat_oblique_down);
        control_seat_up = findViewById(R.id.control_seat_up);
        control_seat_down = findViewById(R.id.control_seat_down);
        control_seat_left = findViewById(R.id.control_seat_left);
        control_seat_right = findViewById(R.id.control_seat_right);
        control_seat_oblique_left.setOnTouchListener(this);
        control_seat_oblique_down.setOnTouchListener(this);
        control_seat_up.setOnTouchListener(this);
        control_seat_down.setOnTouchListener(this);
        control_seat_left.setOnTouchListener(this);
        control_seat_right.setOnTouchListener(this);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.control_light_off:
                lowBeamLight = 0;
                widthLight = 0;
                rFogLight = 0;
                rWidthLight = 0;
                byteValid = 1;
                VehicleService.service.MakeAndSendMessage(0, senLightLockMsgToService());
                mHandler.sendEmptyMessage(SenValidDataToService);
                break;
            case R.id.control_light_rear_rog:
                rFogLight = control_light_rear_rog.isSelected() == false ? 1 : 0;
                byteValid = 1;
                VehicleService.service.MakeAndSendMessage(0, senLightLockMsgToService());
                mHandler.sendEmptyMessage(SenValidDataToService);
                break;
            case R.id.control_light_home:
                control_light_home.setSelected(!control_light_home.isSelected());
                goHomeLight = control_light_home.isSelected() == false ? 0 : 1;
                byteValid = 1;
                JSONUtil.getInstance().updateJson(JSONKeyName.GoHomeLight,goHomeLight);
                VehicleService.service.MakeAndSendMessage(0,senLightLockMsgToService());
                mHandler.sendEmptyMessage(SenValidDataToService);
                break;
            case R.id.control_pilot_driving_standard://标准驾驶模式
                clickDrivingMode = 1;
                updateDrivingModeView();
                mHandler.sendEmptyMessage(SenDrivingModeToService);
                break;
            case R.id.control_pilot_driving_motion://运动驾驶模式
                clickDrivingMode = 2;
                updateDrivingModeView();
                mHandler.sendEmptyMessage(SenDrivingModeToService);
                break;
            case R.id.control_pilot_driving_economic://经济驾驶模式
                clickDrivingMode = 3;
                updateDrivingModeView();
                mHandler.sendEmptyMessage(SenDrivingModeToService);
                break;
            case R.id.control_pilot_energy_low:
                clickErvLevel = 0;
                updateEnergyLevelView();
                mHandler.sendEmptyMessage(SenEnergyModeToService);
                break;
            case R.id.control_pilot_energy_mid:
                clickErvLevel = 1;
                updateEnergyLevelView();
                mHandler.sendEmptyMessage(SenEnergyModeToService);
                break;
            case R.id.control_pilot_energy_high:
                clickErvLevel = 2;
                updateEnergyLevelView();
                mHandler.sendEmptyMessage(SenEnergyModeToService);
                break;
        }
    }

    @Override
    public boolean onTouch(View view, MotionEvent motionEvent) {
        Log.d(TAG,"getAction =" +  motionEvent.getAction() );
        int rawX = (int) motionEvent.getRawX();
        int rawY = (int) motionEvent.getRawY();
        if(motionEvent.getAction() == MotionEvent.ACTION_DOWN) {
            control_cl.requestDisallowInterceptTouchEvent(true);
            byteValid = 1;
            if (DataInfo.getInstance().getDataParamerInfo(ModuleKey.VCU_Car_Speed) != null) {
                carSpeed = (double) DataInfo.getInstance().getDataParamerInfo(ModuleKey.VCU_Car_Speed);
            }
            Log.d(TAG,"carSpeed == " +carSpeed);
            switch (view.getId()) {
                case R.id.control_seat_oblique_left:
                    if(carSpeed > 5) {
                        toastUtils.Indefinite(this,getResources().getString(R.string.driving_tip),5000)
                                .setToastBackground(Color.WHITE,R.drawable.control_driving_tip_bg)
                                .setToastSize(1080,308,30).show()
                                .show();
                        return false;
                    }
                    driverSeatAdjust = 6;
                    VehicleService.service.MakeAndSendMessage(0, senLightLockMsgToService());
                    break;
                case R.id.control_seat_oblique_down:
                    if(carSpeed > 5) {
                        toastUtils.Indefinite(this,getResources().getString(R.string.driving_tip),5000)
                                .setToastBackground(Color.WHITE,R.drawable.control_driving_tip_bg)
                                .setToastSize(1080,308,30).show()
                                .show();
                        return false;
                    }
                    driverSeatAdjust = 5;
                    VehicleService.service.MakeAndSendMessage(0, senLightLockMsgToService());
                    break;
                case R.id.control_seat_up:
                    if(carSpeed > 5) {
                        toastUtils.Indefinite(this,getResources().getString(R.string.driving_tip),5000)
                                .setToastBackground(Color.WHITE,R.drawable.control_driving_tip_bg)
                                .setToastSize(1080,308,30).show()
                                .show();
                        return false;
                    }
                    driverSeatAdjust = 3;
                    VehicleService.service.MakeAndSendMessage(0, senLightLockMsgToService());
                    break;
                case R.id.control_seat_down:
                    if(carSpeed > 5) {
                        toastUtils.Indefinite(this,getResources().getString(R.string.driving_tip),5000)
                                .setToastBackground(Color.WHITE,R.drawable.control_driving_tip_bg)
                                .setToastSize(1080,308,30).show()
                                .show();
                        return false;
                    }
                    driverSeatAdjust = 4;
                    VehicleService.service.MakeAndSendMessage(0, senLightLockMsgToService());
                    break;
                case R.id.control_seat_left:
                    if(carSpeed > 5) {
                        toastUtils.Indefinite(this,getResources().getString(R.string.driving_tip),5000)
                                .setToastBackground(Color.WHITE,R.drawable.control_driving_tip_bg)
                                .setToastSize(1080,308,30).show()
                                .show();
                        return false;
                    }
                    driverSeatAdjust = 1;
                    VehicleService.service.MakeAndSendMessage(0, senLightLockMsgToService());
                    break;
                case R.id.control_seat_right:
                    if(carSpeed > 5) {
                        toastUtils.Indefinite(this,getResources().getString(R.string.driving_tip),5000)
                                .setToastBackground(Color.WHITE,R.drawable.control_driving_tip_bg)
                                .setToastSize(1080,308,30).show()
                                .show();
                        return false;
                    }
                    driverSeatAdjust = 2;
                    VehicleService.service.MakeAndSendMessage(0, senLightLockMsgToService());
                    break;
                case R.id.control_lock_trunk:
                    if(carSpeed >5) {
                        toastUtils.Indefinite(this,getResources().getString(R.string.driving_tip),5000)
                                .setToastBackground(Color.WHITE,R.drawable.control_driving_tip_bg)
                                .setToastSize(1080,308,30).show()
                                .show();
                        return false;
                    }
                    Log.d(TAG,"control_lock_trunk");

                    if(control_lock_trunk.isClick_enable()) {
                        if(control_lock_trunk.isSelected()) {
                            trunk = 3;
                        }else {
                            trunk = 1;
                        }
                        control_lock_trunk.setClick_enable(false);
                        VehicleService.service.MakeAndSendMessage(0, senLightLockMsgToService());
                        if(mHandler.hasMessages(MsgViewUtil.TrunkTimeout)) {
                            mHandler.removeMessages(MsgViewUtil.TrunkTimeout);
                        }
                        mHandler.sendEmptyMessageDelayed(MsgViewUtil.TrunkTimeout,15000);

                    }

                    break;
                case R.id.control_lock_rearview_mirror:
                    if(control_lock_rearview_mirror.isClick_enable()) {
                        if (control_lock_rearview_mirror.isSelected()) {
                            outerMirror_lf = 2;
                            outerMirror_rf = 2;
                            control_lock_rearview_mirror.setClick_enable(false);
                            VehicleService.service.MakeAndSendMessage(0,senLightLockMsgToService());
                            mHandler.sendEmptyMessageDelayed(MsgViewUtil.OuterMirrorTimeout,10000);
                        } else {
                            if(carSpeed > 11) {
                                toastUtils.Indefinite(this,getResources().getString(R.string.driving_tip),5000)
                                        .setToastBackground(Color.WHITE,R.drawable.control_driving_tip_bg)
                                        .setToastSize(1080,308,30).show()
                                        .show();
                            }else{
                                outerMirror_lf = 1;
                                outerMirror_rf = 1;
                                control_lock_rearview_mirror.setClick_enable(false);
                                VehicleService.service.MakeAndSendMessage(0,senLightLockMsgToService());
                                mHandler.sendEmptyMessageDelayed(MsgViewUtil.OuterMirrorTimeout,10000);
                            }
                        }
                    }
                    break;
                case R.id.control_lock_off:
                    if(control_lock_off.isClick_enable()) {
                        lfWindow_up = 1;
                        lfWindow_down = 0;
                        rfWindow_up = 1;
                        rfWindow_down = 0;
                        lrWindow_up = 1;
                        lrWindow_down = 0;
                        rrWindow_up = 1;
                        rrWindow_down = 0;
                        autoWindow = 1;
                        VehicleService.service.MakeAndSendMessage(0, senLightLockMsgToService());
                        control_lock_off.setClick_enable(false);
                        control_lock_breathable.setClick_enable(false);
                        control_lock_ventilation.setClick_enable(false);
                        control_lock_breathable.setBackground(getResources().getDrawable(R.drawable.control_lock_breathable_o));
                        control_lock_ventilation.setBackground(getResources().getDrawable(R.drawable.control_lock_ventilation_o));
                        mHandler.sendEmptyMessageDelayed(updateWindows,1000);
                    }
                    break;
                case R.id.control_lock_breathable:
                    if(control_lock_breathable.isClick_enable()) {
                        breathable = 1;
                        VehicleService.service.MakeAndSendMessage(0, senLightLockMsgToService());
                        control_lock_off.setClick_enable(false);
                        control_lock_breathable.setClick_enable(false);
                        control_lock_ventilation.setClick_enable(false);
                        control_lock_off.setBackground(getResources().getDrawable(R.drawable.control_lock_off_o));
                        control_lock_ventilation.setBackground(getResources().getDrawable(R.drawable.control_lock_ventilation_o));
                        breathable = 0;
                        VehicleService.service.MakeAndSendMessage(0, senLightLockMsgToService());
                        mHandler.sendEmptyMessageDelayed(updateWindows,1000);
                    }
                    break;
                case R.id.control_lock_ventilation:
                    if(control_lock_ventilation.isClick_enable()) {
                        lfWindow_up = 0;
                        lfWindow_down = 1;
                        rfWindow_up = 0;
                        rfWindow_down = 1;
                        lrWindow_up = 0;
                        lrWindow_down = 1;
                        rrWindow_up = 0;
                        rrWindow_down = 1;
                        autoWindow = 1;
                        VehicleService.service.MakeAndSendMessage(0, senLightLockMsgToService());
                        control_lock_off.setClick_enable(false);
                        control_lock_breathable.setClick_enable(false);
                        control_lock_ventilation.setClick_enable(false);
                        control_lock_off.setBackground(getResources().getDrawable(R.drawable.control_lock_off_o));
                        control_lock_breathable.setBackground(getResources().getDrawable(R.drawable.control_lock_breathable_o));
                        mHandler.sendEmptyMessageDelayed(updateWindows,1000);
                    }
                    break;
                case R.id.control_lock_unlock:
                    if (control_lock_unlock.isSelected()) {
                        lfDoorLock = 1;
                        rfDoorLock = 1;
                        lrDoorLock = 1;
                        rrDoorLock = 1;
                    }else {
                        lfDoorLock = 2;
                        rfDoorLock = 2;
                        lrDoorLock = 2;
                        rrDoorLock = 2;
                    }
                    VehicleService.service.MakeAndSendMessage(0,senLightLockMsgToService());
                    break;
                case R.id.control_auto_light_bg:
                    int width = (int) (view.getWidth()/3);
                    if(motionEvent.getX()<width){
                        control_auto_light_thumb.startAnimation(newTranslateAnimation(0));
                        autolightThumbX=0;
                        widthLight = 1;
                        lowBeamLight = 0;
                        autoLight = 0;
                    }else  if(motionEvent.getX()>width&&motionEvent.getX()<width*2){
                        control_auto_light_thumb.startAnimation(newTranslateAnimation(158));
                        autolightThumbX=158;
                        lowBeamLight = 1;
                        autoLight = 0;
                    }else {
                        control_auto_light_thumb.startAnimation(newTranslateAnimation(316));
                        autolightThumbX=316;
                        lowBeamLight = 1;
                        autoLight = 1;
                    }
                    VehicleService.service.MakeAndSendMessage(0,senLightLockMsgToService());
                    JSONUtil.getInstance().updateJson(JSONKeyName.AutoLightThumbX,autolightThumbX);
                    JSONUtil.getInstance().updateJson(JSONKeyName.AutoLight,autoLight);
                    mHandler.sendEmptyMessage(SenValidDataToService);
                    break;
            }
        } else if (motionEvent.getAction() == MotionEvent.ACTION_UP) {
            control_cl.requestDisallowInterceptTouchEvent(false);
            byteValid = 1;
            switch (view.getId()) {
                case R.id.control_seat_oblique_left:
                case R.id.control_seat_oblique_down:
                case R.id.control_seat_up:
                case R.id.control_seat_down:
                case R.id.control_seat_left:
                case R.id.control_seat_right:
                    driverSeatAdjust=0;
                    VehicleService.service.MakeAndSendMessage(0, senLightLockMsgToService());
                    break;
                case R.id.control_lock_trunk:
                    trunk = 0;
                    VehicleService.service.MakeAndSendMessage(0,senLightLockMsgToService());
                    break;
                case R.id.control_lock_off:
                    lfWindow_up = 0;
                    rfWindow_up = 0;
                    lrWindow_up = 0;
                    rrWindow_up = 0;
                    autoWindow = 0;
                    VehicleService.service.MakeAndSendMessage(0,senLightLockMsgToService());
                    break;
                case R.id.control_lock_breathable:
                    breathable = 0;
                    VehicleService.service.MakeAndSendMessage(0,senLightLockMsgToService());
                    break;
                case R.id.control_lock_ventilation:
                    lfWindow_down = 0;
                    rfWindow_down = 0;
                    lrWindow_down = 0;
                    rrWindow_down = 0;
                    autoWindow = 0;
                    VehicleService.service.MakeAndSendMessage(0,senLightLockMsgToService());
                    break;
                case R.id.control_lock_unlock:
                    lfDoorLock = 0;
                    rfDoorLock = 0;
                    lrDoorLock = 0;
                    rrDoorLock = 0;
                    VehicleService.service.MakeAndSendMessage(0,senLightLockMsgToService());
                    break;
            }
            mHandler.sendEmptyMessage(SenValidDataToService);
        } else  if (motionEvent.getAction() == MotionEvent.ACTION_MOVE) {
            switch (view.getId()) {
                case R.id.control_seat_oblique_left:
                case R.id.control_seat_oblique_down:
                case R.id.control_seat_up:
                case R.id.control_seat_down:
                case R.id.control_seat_left:
                case R.id.control_seat_right:
                    if(!isTouchPointInView(view,rawX,rawY)) {
                        driverSeatAdjust=0;
                        byteValid = 1;
                        VehicleService.service.MakeAndSendMessage(0, senLightLockMsgToService());
                        control_cl.requestDisallowInterceptTouchEvent(false);
                        mHandler.sendEmptyMessage(SenValidDataToService);
                    }
                    break;
                case R.id.control_lock_trunk:
                    if(!isTouchPointInView(view,rawX,rawY)) {
                        trunk = 0;
                        byteValid = 1;
                        VehicleService.service.MakeAndSendMessage(0,senLightLockMsgToService());
                        control_cl.requestDisallowInterceptTouchEvent(false);
                        mHandler.sendEmptyMessage(SenValidDataToService);
                    }
                    break;
                case R.id.control_lock_off:
                    if(!isTouchPointInView(view,rawX,rawY)) {
                        lfWindow_up = 0;
                        rfWindow_up = 0;
                        lrWindow_up = 0;
                        rrWindow_up = 0;
                        autoWindow = 0;
                        byteValid = 1;
                        VehicleService.service.MakeAndSendMessage(0,senLightLockMsgToService());
                        control_cl.requestDisallowInterceptTouchEvent(false);
                        mHandler.sendEmptyMessage(SenValidDataToService);
                    }
                    break;
                case R.id.control_lock_breathable:
                    if(!isTouchPointInView(view,rawX,rawY)) {
                        breathable = 0;
                        byteValid = 1;
                        VehicleService.service.MakeAndSendMessage(0,senLightLockMsgToService());
                        control_cl.requestDisallowInterceptTouchEvent(false);
                        mHandler.sendEmptyMessage(SenValidDataToService);
                    }
                    break;
                case R.id.control_lock_ventilation:
                    if(!isTouchPointInView(view,rawX,rawY)) {
                        lfWindow_down = 0;
                        rfWindow_down = 0;
                        lrWindow_down = 0;
                        rrWindow_down = 0;
                        autoWindow = 0;
                        byteValid = 1;
                        VehicleService.service.MakeAndSendMessage(0,senLightLockMsgToService());
                        control_cl.requestDisallowInterceptTouchEvent(false);
                        mHandler.sendEmptyMessage(SenValidDataToService);
                    }
                    break;
                case R.id.control_lock_unlock:
                    if(!isTouchPointInView(view,rawX,rawY)) {
                        lfDoorLock = 0;
                        rfDoorLock = 0;
                        lrDoorLock = 0;
                        rrDoorLock = 0;
                        byteValid = 1;
                        VehicleService.service.MakeAndSendMessage(0,senLightLockMsgToService());
                        control_cl.requestDisallowInterceptTouchEvent(false);
                        mHandler.sendEmptyMessage(SenValidDataToService);
                    }
                    break;
            }
        } else if(motionEvent.getAction() == MotionEvent.ACTION_CANCEL) {
            control_cl.requestDisallowInterceptTouchEvent(false);
            byteValid = 1;
            switch (view.getId()) {
                case R.id.control_seat_oblique_left:
                case R.id.control_seat_oblique_down:
                case R.id.control_seat_up:
                case R.id.control_seat_down:
                case R.id.control_seat_left:
                case R.id.control_seat_right:
                    driverSeatAdjust = 0;
                    VehicleService.service.MakeAndSendMessage(0, senLightLockMsgToService());
                    break;
                case R.id.control_lock_trunk:
                    trunk = 0;
                    VehicleService.service.MakeAndSendMessage(0, senLightLockMsgToService());
                    break;
                case R.id.control_lock_off:
                    lfWindow_up = 0;
                    rfWindow_up = 0;
                    lrWindow_up = 0;
                    rrWindow_up = 0;
                    autoWindow = 0;
                    VehicleService.service.MakeAndSendMessage(0, senLightLockMsgToService());
                    break;
                case R.id.control_lock_breathable:
                    breathable = 0;
                    VehicleService.service.MakeAndSendMessage(0, senLightLockMsgToService());
                    break;
                case R.id.control_lock_ventilation:
                    lfWindow_down = 0;
                    rfWindow_down = 0;
                    lrWindow_down = 0;
                    rrWindow_down = 0;
                    autoWindow = 0;
                    VehicleService.service.MakeAndSendMessage(0, senLightLockMsgToService());
                    break;
                case R.id.control_lock_unlock:
                    lfDoorLock = 0;
                    rfDoorLock = 0;
                    lrDoorLock = 0;
                    rrDoorLock = 0;
                    VehicleService.service.MakeAndSendMessage(0, senLightLockMsgToService());
                    break;
            }
            mHandler.sendEmptyMessage(SenValidDataToService);
        }
        return false;
    }

    private boolean isTouchPointInView(View view, int x, int y) {
        if (view == null) {
            return false;
        }
        int[] location = new int[2];
        view.getLocationOnScreen(location);
        int left = location[0];
        int top = location[1];
        int right = left + view.getMeasuredWidth();
        int bottom = top + view.getMeasuredHeight();
        if (y >= top && y <= bottom && x >= left  && x <= right) {
            return true;
        }
        return false;
    }

    private AnimationSet newTranslateAnimation(int x){
        control_auto_light_thumb.setImageLevel(1);
        animationSet = new AnimationSet(true);
        TranslateAnimation translateAnimation = new TranslateAnimation(autolightThumbX,x,0,0);
        //3秒完成动画
        translateAnimation.setDuration(200);
        //如果fillAfter的值为真的话，动画结束后，控件停留在执行后的状态
        animationSet.setFillAfter(true);
        //将AlphaAnimation这个已经设置好的动画添加到 AnimationSet中
        animationSet.addAnimation(translateAnimation);
        control_auto_light_thumb.startAnimation(animationSet);
        control_auto_light_content.setVisibility(View.VISIBLE);
        return animationSet;
    }

    @Override
    public void sendMsg(int what, Object arg) {
        Message msg = null;
        if (mHandler != null) {
            msg = mHandler.obtainMessage(what);
            msg.what = what;
            msg.obj = arg;
            mHandler.sendMessage(msg);
        } else {
            L.d(TAG, "mHandler is null");
        }
    }
    public final int updateWindows = 2;
    public final int SenValidDataToService = 3;
    public final int ByteValidDelay = 4;
    public final int clickDrivingModeDelayed = 5;
    public final int SenDrivingModeToService = 6;
    public final int clickEnergyModeDelayed = 7;
    public final int SenEnergyModeToService = 8;
    public Handler mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case MsgViewUtil.Update_Light_State:
                    updateLightView();
                    break;
                case MsgViewUtil.Update_Window_View:
                    removeMessages(updateWindows);
                    sendEmptyMessageDelayed(updateWindows,500);
                    break;
                case updateWindows:
                    updateWindowPosView();
                    break;
                case MsgViewUtil.Update_DoorLock_State:
                    updateDoorLockView();
                    break;
                case MsgViewUtil.Update_OuterMirror_State:
                    updateOuterMirrorView();
                    break;
                case MsgViewUtil.Update_Trunk_State:
                    updateTrunkView();
                    break;
                case MsgViewUtil.Update_Driving_Mode:
                    updateDrivingModeView();
                    break;
                case MsgViewUtil.Update_Energy_Mode:
                    updateEnergyLevelView();
                    break;
                case MsgViewUtil.Update_Seat_View:
                    updateSeatView();
                    break;
                case MsgViewUtil.UpateDriverSeatWindWarmLv:
                    updateDriverSeatWindWarmLv();
                    break;
                case MsgViewUtil.UpdateWiperStatus:
                    updateWiper();
                    break;
                case MsgViewUtil.UpdateWalkWarn:
                    updateWalkWarn();
                    break;
                case MsgViewUtil.UpdateSafetyBelt:
                    updateSafetyBelt();
                    break;
                 case MsgViewUtil.OuterMirrorTimeout:
                     outerMirror_lf =0;
                     outerMirror_rf =0;
                     byteValid = 1;
                     control_lock_rearview_mirror.setClick_enable(true);
                     VehicleService.service.MakeAndSendMessage(0,senLightLockMsgToService());
                     mHandler.sendEmptyMessageDelayed(ByteValidDelay,500);
                     break;
                case SenValidDataToService:
                    if(hasMessages(ByteValidDelay)) {
                        removeMessages(ByteValidDelay);
                    }
                    mHandler.sendEmptyMessageDelayed(ByteValidDelay,500);
                    break;
                case ByteValidDelay:
                    byteValid = 0;
                    VehicleService.service.MakeAndSendMessage(0,senLightLockMsgToService());
                    break;
                case MsgViewUtil.TrunkTimeout:
                    control_lock_trunk.setSelected(!control_lock_trunk.isSelected());
                    control_lock_trunk.setClick_enable(true);
                    break;
                case clickDrivingModeDelayed:
                    JSONUtil.getInstance().updateJson(JSONKeyName.DrivingMode,clickDrivingMode);
                    VehicleService.service.MakeAndSendMessage(1, senPilotMsgToService());
                    break;
                case SenDrivingModeToService:
                    if(hasMessages(clickDrivingModeDelayed)) {
                        removeMessages(clickDrivingModeDelayed);
                    }
                    mHandler.sendEmptyMessageDelayed(clickDrivingModeDelayed,500);
                    break;
                case clickEnergyModeDelayed:
                    JSONUtil.getInstance().updateJson(JSONKeyName.EnergyRecovery,clickErvLevel);
                    VehicleService.service.MakeAndSendMessage(1, senPilotMsgToService());
                    break;
                case SenEnergyModeToService:
                    if(hasMessages(clickEnergyModeDelayed)) {
                        removeMessages(clickEnergyModeDelayed);
                    }
                    mHandler.sendEmptyMessageDelayed(clickEnergyModeDelayed,500);
                    break;
            }
        }
    };

    private void updateLightView() {
         Log.d(TAG,"updateLightView");
         highBeamLight = getMapVal(ModuleKey.BCM_HighBeamLight_Status);
         lowBeamLight = getMapVal(ModuleKey.BCM_LowBeamLight_Status);
         widthLight = getMapVal(ModuleKey.BCM_FrontTailLight_Status);
         rFogLight = getMapVal(ModuleKey.BCM_RearFogLight_Status);
         rWidthLight = getMapVal(ModuleKey.BCM_RearTailLight_Status);

        autolightThumbX = (int) JSONUtil.getInstance().getMileageJSON(JSONKeyName.AutoLightThumbX,0);
        control_auto_light_thumb.startAnimation(newTranslateAnimation(autolightThumbX));

        //先开启近光灯、远光灯才能打开后雾灯
        if ((lowBeamLight == 1) || (highBeamLight == 1)) {
            control_light_rear_rog.setEnabled(true);
            control_light_rear_rog.setBackground(getResources().getDrawable(R.drawable.bt_control_light_rear_rog));
            control_light_rear_rog.setSelected(rFogLight == 0 ? false : true);
            lowBeamLight = 1;
        } else {
            control_light_rear_rog.setEnabled(false);
            control_light_rear_rog.setBackground(getResources().getDrawable(R.drawable.control_light_rear_rog_o));
        }


        //近光灯、示宽灯、前雾灯、前示宽灯同为0时，OFF
        if (lowBeamLight == 0 && widthLight == 0 && rFogLight == 0 && rWidthLight == 0) {
            control_light_off.setSelected(true);
            control_auto_light_content.setVisibility(View.GONE);
            control_auto_light_thumb.setImageLevel(0);
        } else {
            control_light_off.setSelected(false);
        }

    }

    private void updateDoorLockView() {
        Log.d(TAG,"updateDoorLockView");
        int lfDoor = (int) JSONUtil.getInstance().getMileageJSON(JSONKeyName.DoorLockLF,0);
        int rfDoor = (int) JSONUtil.getInstance().getMileageJSON(JSONKeyName.DoorLockRF,0);
        int lrDoor = (int) JSONUtil.getInstance().getMileageJSON(JSONKeyName.DoorLockLR,0);
        int rrDoor = (int) JSONUtil.getInstance().getMileageJSON(JSONKeyName.DoorLockRR,0);

        //左前、右前、左后、右后门锁同为0时上锁，否则为解锁
        if (lfDoor == 0 && rfDoor == 0 && lrDoor == 0 && rrDoor == 0) {
            control_lock_unlock.setSelected(false);
        } else {
            control_lock_unlock.setSelected(true);
        }
    }

    private void updateWindowPosView() {
        Log.d(TAG,"updateWindowPosView");
        int lfWindowPos = (int) JSONUtil.getInstance().getMileageJSON(JSONKeyName.WindowPosLF, 0);
        int rfWindowPos = (int) JSONUtil.getInstance().getMileageJSON(JSONKeyName.WindowPosRF, 0);
        int lrWindowPos = (int) JSONUtil.getInstance().getMileageJSON(JSONKeyName.WindowPosLR, 0);
        int rrWindowPos = (int) JSONUtil.getInstance().getMileageJSON(JSONKeyName.WindowPosRR, 0);
        if(lfWindowPos == 0 && rfWindowPos==0 && lrWindowPos==0 && rrWindowPos == 0) {
            control_lock_off.setSelected(false);
            control_lock_off.setBackground(getResources().getDrawable(R.drawable.bt_control_lock_off));
            control_lock_breathable.setBackground(getResources().getDrawable(R.drawable.control_lock_breathable_o));
            control_lock_ventilation.setBackground(getResources().getDrawable(R.drawable.bt_control_lock_ventilation));
            control_lock_ventilation.setSelected(true);

            control_lock_off.setClick_enable(true);
            control_lock_breathable.setClick_enable(false);
            control_lock_ventilation.setClick_enable(false);
        }else if(lfWindowPos >= 14 && rfWindowPos>=14 && lrWindowPos>=14 && rrWindowPos >= 14) {
            control_lock_off.setSelected(true);
            control_lock_breathable.setBackground(getResources().getDrawable(R.drawable.bt_control_lock_breathable));
            control_lock_breathable.setSelected(false);

            control_lock_ventilation.setBackground(getResources().getDrawable(R.drawable.bt_control_lock_ventilation));
            control_lock_ventilation.setSelected(false);

            control_lock_off.setClick_enable(false);
            control_lock_breathable.setClick_enable(true);
            control_lock_ventilation.setClick_enable(true);
        } else  {
            control_lock_off.setSelected(false);
            control_lock_off.setBackground(getResources().getDrawable(R.drawable.bt_control_lock_off));
            control_lock_ventilation.setSelected(false);
            control_lock_ventilation.setBackground(getResources().getDrawable(R.drawable.bt_control_lock_ventilation));
            control_lock_breathable.setBackground(getResources().getDrawable(R.drawable.bt_control_lock_breathable));
            control_lock_breathable.setSelected(true);
            control_lock_off.setClick_enable(true);
            control_lock_breathable.setClick_enable(false);
            control_lock_ventilation.setClick_enable(true);
        }
    }

    private void updateOuterMirrorView() {
        int outerMirror = (int) JSONUtil.getInstance().getMileageJSON(JSONKeyName.OuterMirrorState,0);
        Log.d(TAG,"updateOuterMirrorView outerMirror: " + outerMirror);
        if(outerMirror == 0 || outerMirror == 3) {
            if(mHandler.hasMessages(MsgViewUtil.OuterMirrorTimeout)) {
                mHandler.removeMessages(MsgViewUtil.OuterMirrorTimeout);
                mHandler.sendEmptyMessage(MsgViewUtil.OuterMirrorTimeout);
            }
            control_lock_rearview_mirror.setClick_enable(true);
        }
        control_lock_rearview_mirror.setSelected(outerMirror == 0 ? true : false);
    }

    private void updateTrunkView() {
        int mTrunk = (int) JSONUtil.getInstance().getMileageJSON(JSONKeyName.BackDoorState,2);
        Log.d(TAG,"updateTrunkView mTrunk: " + mTrunk);
        if(mTrunk == 1) {
            control_lock_trunk.setSelected(true);
            control_lock_trunk.setClick_enable(true);
            if(mHandler.hasMessages(MsgViewUtil.TrunkTimeout)) {
                mHandler.removeMessages(MsgViewUtil.TrunkTimeout);
            }
        }else if(mTrunk == 2) {
            control_lock_trunk.setSelected(false);
            control_lock_trunk.setClick_enable(true);
            if(mHandler.hasMessages(MsgViewUtil.TrunkTimeout)) {
                mHandler.removeMessages(MsgViewUtil.TrunkTimeout);
            }
        }else if(mTrunk == 3) {
            control_lock_trunk.setSelected(true);
            control_lock_trunk.setClick_enable(false);
        }
    }

    /**
     * 驾驶模式
     * @return
     */
    private void updateDrivingModeView() {
        Button[] drivingMode = {control_pilot_driving_standard, control_pilot_driving_motion, control_pilot_driving_economic};

        if (clickDrivingMode > drivingMode.length) {
            return;
        }
        for (int i = 0; i < drivingMode.length; i++) {
            if (i == (clickDrivingMode - 1)) {
                drivingMode[i].setSelected(true);
            } else {
                drivingMode[i].setSelected(false);
            }
        }
    }

    /**
     * 能量回收等级
     * @return
     */
    private void updateEnergyLevelView() {
        Button [] control_pilot={control_pilot_energy_low,control_pilot_energy_mid,control_pilot_energy_high};

        if (clickErvLevel >= control_pilot.length) {
            return;
        }
        for (int i = 0; i < control_pilot.length; i++) {
            if (i == clickErvLevel) {
                control_pilot[i].setSelected(true);
            } else {
                control_pilot[i].setSelected(false);
            }
        }
    }

    private int getMapVal(String key){
        int val=0;
        if(DataInfo.getInstance().getDataParamerInfo(key) != null) {
            val = (int) DataInfo.getInstance().getDataParamerInfo(key) ;
        }
        return val;
    }

    /**
     * ID 0x1d000201 驾驶模式、回收能量的协议拼接
     * @return
     */
    public byte[] senPilotMsgToService() {
        byte[] mSendData = {0x1d, 0x00, 0x02, 0x01, 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07};
        int modeId = clickDrivingMode  | (clickErvLevel <<4);
        mSendData[4] = (byte) modeId;
        return mSendData;
    }

    /**
     * ID 0x0630 车灯、车锁的协议拼接
     * @return
     */
    private byte[] senLightLockMsgToService() {
        byte[] mSendData = {0x06, 0x30, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
        mSendData[2] = (byte) (breathable | (lowBeamLight << 1) | (autoLight << 2) | (widthLight << 3) | (goHomeLight << 4) | (driverSeatWindLevel << 5));
        mSendData[3] = (byte) (driverSeat_WarnLevel | (frontWiperGear << 3) | (rFogLight << 5) | (rWidthLight << 6) | (rearWiperSw << 7));
        mSendData[4] = (byte) (trunk | (lfWindow_up << 2) | (lfWindow_down << 3) | (rfWindow_up << 4) | (rfWindow_down << 5) | (lrWindow_up << 6) | (lrWindow_down << 7));
        mSendData[5] = (byte) (rrWindow_up | (rrWindow_down << 1) | (lfDoorLock << 2) | (rfDoorLock << 4) | (outerMirror_lf << 6));
        mSendData[6] = (byte) (outerMirror_rf | (autoWindow << 6) | (frontHorn << 7));
        mSendData[7] = (byte) byteValid;
        mSendData[8] = (byte) ((walkWarnSw << 3) | (walkWarnEn << 4) | (autoSafetyBeltSw << 6));
        mSendData[9] = (byte) (driverSeatAdjust | (rearSafetyBeltWarnSw << 3) | (lrDoorLock << 4) | (rrDoorLock << 6));
        return mSendData;
    }

    private void updateSeatView() {
        Log.d(TAG,"updateSeatView");
        int seatMoveDirect =(int) JSONUtil.getInstance().getMileageJSON(JSONKeyName.SeatMoveDirect,0);
        int seatFRPosition =(int)  JSONUtil.getInstance().getMileageJSON(JSONKeyName.SeatFRPos,0);
        int seatUDPosition =(int) JSONUtil.getInstance().getMileageJSON(JSONKeyName.SeatUDPos,0);
        int seatBackPositon = (int) JSONUtil.getInstance().getMileageJSON(JSONKeyName.SeatBackPos,0);


        moveX = (float) (100-seatFRPosition*2.2);
        moveY = (float) (70-seatUDPosition*1.4);
        moveZ = (float) (seatBackPositon*0.55-10);

        switch (seatMoveDirect) {
            case 0: //no action
                setSeatDefault();
                break;
            case 1: //front
                setSeatMoveFrontRear();
                break;
            case 2: //rear
                setSeatMoveFrontRear();
                break;
            case 3: //up
                setSeatMoveUpDown();
                break;
            case 4: //down
                setSeatMoveUpDown();
                break;
            case 5: //背前
                setSeatMoveBack();
                break;
            case 6: //背后
                setSeatMoveBack();
                break;
        }
    }

    private void setSeatDefault() {
        setSeatMoveX();
        setSeatMoveY();
        setSeatMoveZ();
        control_seat_iv.setSeatMovePositon(moveX,moveY,moveZ);
        control_up_down_cl.setTranslationX(moveX);
        control_left_right_cl.setTranslationY(moveY);
        control_oblique_cl.setTranslationY(moveY);
    }

    private void setSeatMoveX() {
        if(moveX <= -120) {
            moveX = -120;
            setBtnEnabled(control_seat_left,false,getResources().getDrawable(R.drawable.control_seat_left_o));
            setBtnEnabled(control_seat_right,true,getResources().getDrawable(R.drawable.bt_control_seat_right));
        }else if(moveX >= 100) {
            moveX = 100;
            setBtnEnabled(control_seat_right,false,getResources().getDrawable(R.drawable.control_seat_right_o));
            setBtnEnabled(control_seat_left,true,getResources().getDrawable(R.drawable.bt_control_seat_left));
        }else {
            if(!control_seat_left.isClick_enable()) {
                setBtnEnabled(control_seat_left,true,getResources().getDrawable(R.drawable.bt_control_seat_left));
            }
            if(!control_seat_right.isClick_enable()) {
                setBtnEnabled(control_seat_right,true,getResources().getDrawable(R.drawable.bt_control_seat_right));
            }
        }
    }

    private void setSeatMoveY() {
        if(moveY <= -70) {
            moveY = -70;
            setBtnEnabled(control_seat_up,false,getResources().getDrawable(R.drawable.control_seat_up_o));
            setBtnEnabled(control_seat_down,true,getResources().getDrawable(R.drawable.bt_control_seat_down));
        }else if(moveY >= 70) {
            moveY = 70;
            setBtnEnabled(control_seat_down,false,getResources().getDrawable(R.drawable.control_seat_down_o));
            setBtnEnabled(control_seat_up,true,getResources().getDrawable(R.drawable.bt_control_seat_up));
        }else {
            if(!control_seat_up.isClick_enable()) {
                setBtnEnabled(control_seat_up,true,getResources().getDrawable(R.drawable.bt_control_seat_up));
            }
            if(!control_seat_down.isClick_enable()) {
                setBtnEnabled(control_seat_down,true,getResources().getDrawable(R.drawable.bt_control_seat_down));
            }
        }
    }

    private  void setSeatMoveZ() {
        if(moveZ <= -10) {
            moveZ = -10;
            setBtnEnabled(control_seat_oblique_left,false,getResources().getDrawable(R.drawable.control_seat_left_o));
            setBtnEnabled(control_seat_oblique_down,true,getResources().getDrawable(R.drawable.bt_control_seat_down));
        }else if(moveZ >= 45) {
            moveZ = 45;
            setBtnEnabled(control_seat_oblique_down,false,getResources().getDrawable(R.drawable.control_seat_down_o));
            setBtnEnabled(control_seat_oblique_left,true,getResources().getDrawable(R.drawable.bt_control_seat_left));
        }else {
            if(!control_seat_oblique_left.isClick_enable()) {
                setBtnEnabled(control_seat_oblique_left,true,getResources().getDrawable(R.drawable.bt_control_seat_left));
            }
            if(!control_seat_oblique_down.isClick_enable()) {
                setBtnEnabled(control_seat_oblique_down,true,getResources().getDrawable(R.drawable.bt_control_seat_down));
            }
        }
    }

    private void setSeatMoveFrontRear() {
        setSeatMoveX();
        control_seat_iv.setSeatMovePositon(moveX,moveY,moveZ);
        control_up_down_cl.setTranslationX(moveX);
    }

    private void setSeatMoveBack() {
        setSeatMoveZ();
        control_seat_iv.setSeatMovePositon(moveX,moveY,moveZ);
    }

    private void setSeatMoveUpDown(){
        setSeatMoveY();
        control_seat_iv.setSeatMovePositon(moveX,moveY,moveZ);
        control_oblique_cl.setTranslationY(moveY);
        control_left_right_cl.setTranslationY(moveY);
    }

    private void setBtnEnabled(ButtonWindows btn, boolean isEnabled,Drawable drawable) {
        btn.setClick_enable(isEnabled);
        btn.setBackground(drawable);
    }

    private void updateView() {
        updateLightView();
        updateDoorLockView();
        updateWindowPosView();
        updateTrunkView();
        updateDrivingModeView();
        updateEnergyLevelView();
        updateSeatView();
        updateDriverSeatWindWarmLv();
        updateWiper();
        updateWalkWarn();
        updateSafetyBelt();
        updateOuterMirrorView();
    }

    private void updateDriverSeatWindWarmLv() {
        driverSeatWindLevel =  getMapVal(ModuleKey.BCM_DRIVERSEAT_WINDLV);
        driverSeat_WarnLevel =  getMapVal(ModuleKey.BCM_DRIVERSEAT_WARNLV);
    }

    private void updateWiper() {
        frontWiperGear = getMapVal(ModuleKey.BCM_FrontWiperGear_Status);
        rearWiperSw = getMapVal(ModuleKey.BCM_RearWiper_Status);
    }

    private void updateWalkWarn() {
        walkWarnSw = getMapVal(ModuleKey.BCM_WALKWARNSW_STATUS);
        walkWarnEn = getMapVal(ModuleKey.BCM_WALKWARNVOICE_STATUS);
    }

    private void updateSafetyBelt() {
         //autoSafetyBeltSw
        int rMidSafetyBelt = getMapVal(ModuleKey.BCM_REARMIDSAFETYBELT);
        int rLeftSafetyBelt = getMapVal(ModuleKey.BCM_REARLEFTSAFETYBELT);
        int rRightSafetyBelt = getMapVal(ModuleKey.BCM_REARRIGHTSAFETYBELT);
        if(rMidSafetyBelt == 1 && rLeftSafetyBelt == 1 && rRightSafetyBelt == 1) {
            rearSafetyBeltWarnSw = 0;
        }else {
            rearSafetyBeltWarnSw = 1;
        }
    }

    Handler updateHandler = new Handler();
    Runnable r = new Runnable() {

        @Override
        public void run() {
            updateHandler.postDelayed(this, 1000);
            int mDrivingMode = getMapVal(ModuleKey.VCUDrivingMode);
            int mVcuErvLevel = getMapVal(ModuleKey.VCU_ERV_Level);
            if (clickDrivingMode != mDrivingMode || clickErvLevel != mVcuErvLevel) {
                VehicleService.service.MakeAndSendMessage(1, senPilotMsgToService());
            }
            updateDrivingModeView();
            updateEnergyLevelView();
        }
    };

}
